package com.wpzf.ndhc.service.impl;

import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.ndhc.domain.WpCjTbhcjgDTO;
import com.wpzf.ndhc.mapper.WpzfNdhcZgTbTaskMapper;
import com.wpzf.ndhc.service.WpzfNdhcZgTbTaskService;
import com.wpzf.ndhc.utils.TableUtil;
import com.wpzf.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class WpzfNdhcZgTbTaskServiceImpl implements WpzfNdhcZgTbTaskService {

    @Autowired
    private WpzfNdhcZgTbTaskMapper wpzfNdhcZgTbTaskMapper;

    @Autowired
    private TokenService tokenService;

    @Override
    public Integer zgSubmit(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        List<Map<String,Object>> list = wpzfNdhcZgTbTaskMapper.queryZgtbByTbbh(parms);
        if (list!=null) {
            wpzfNdhcZgTbTaskMapper.updateZgtbState(parms);
        }
        parms.put("submitdate",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        return wpzfNdhcZgTbTaskMapper.zgSubmit(parms);
    }

    @Override
    public int update_unit_zgtb_state(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.update_unit_zgtb_state(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_zgtb_pch(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_zgtb_pch(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_user_dzgtb_list(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_user_dzgtb_list(parms);
    }

    @Override
    public Integer query_unit_user_dzgtb_list_count(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_user_dzgtb_list_count(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_user_dzgtb_leftlist(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_user_dzgtb_leftlist(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_user_yzgtb_list(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_user_yzgtb_list(parms);
    }

    @Override
    public Integer query_unit_user_yzgtb_list_count(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_user_yzgtb_list_count(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_user_yzgtb_leftlist(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_user_yzgtb_leftlist(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_pch(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_pch(parms);
    }

    @Override
    public List<Map<String, Object>> query_unit_dzgtb_list(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_dzgtb_list(parms);
    }

    @Override
    public int query_unit_dzgtb_list_count(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        Integer count=0;
        Map<String, Object> map = wpzfNdhcZgTbTaskMapper.query_unit_dzgtb_list_count(parms);
        if (map != null) {
            count = ((Number) map.get("cnt")).intValue();
        }
        return count;
    }

    @Override
    public List<Map<String, Object>> query_unit_yzgtb_list(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_yzgtb_list(parms);
    }

    @Override
    public int query_unit_yzgtb_list_count(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        Integer count=0;
        Map<String, Object> map = wpzfNdhcZgTbTaskMapper.query_unit_yzgtb_list_count(parms);
        if (map != null) {
            count = ((Number) map.get("cnt")).intValue();
        }
        return count;
    }

    @Override
    public Map<String, Object> query_unit_zgtbbyid(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_unit_zgtbbyid(parms);
    }

    @Override
    public Integer update_unit_zgtb(Map<String, Object> parms) {
        return wpzfNdhcZgTbTaskMapper.update_unit_zgtb(parms);
    }

    @Override
    public List<Map<String, Object>> query_jl_zgtb_pch(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_zgtb_pch(parms);
    }

    @Override
    public List<Map<String, Object>> query_jl_user_dzgtb_list(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_user_dzgtb_list(parms);
    }

    @Override
    public Integer query_jl_user_dzgtb_list_count(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_user_dzgtb_list_count(parms);
    }

    @Override
    public List<Map<String, Object>> query_jl_user_dzgtb_leftlist(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_user_dzgtb_leftlist(parms);
    }

    @Override
    public List<Map<String, Object>> query_jl_user_yzgtb_list(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_user_yzgtb_list(parms);
    }

    @Override
    public Integer query_jl_user_yzgtb_list_count(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_user_yzgtb_list_count(parms);
    }

    @Override
    public List<Map<String, Object>> query_jl_user_yzgtb_leftlist(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.query_jl_user_yzgtb_leftlist(parms);
    }

    @Override
    public WpCjTbhcjgDTO queryZgtb(Map<String, Object> parms) {
        parms = TableUtil.TableName(parms);
        return wpzfNdhcZgTbTaskMapper.queryZgtb(parms);
    }














    @Override
    public List<Map<String, Object>> getPch(Map<String, Object> param) {
        return wpzfNdhcZgTbTaskMapper.getPch(param);
    }

    @Override
    public List<Map<String, Object>> getSheng(Map<String, Object> param) {
        return wpzfNdhcZgTbTaskMapper.getSheng(param);
    }

    @Override
    public List<Map<String, Object>> getXian(Map<String, Object> param) {
        return wpzfNdhcZgTbTaskMapper.getXian(param);
    }

    @Override
    public List<Map<String, Object>> queryDkTask(Map<String, Object> param) {
        return wpzfNdhcZgTbTaskMapper.queryDkTask(param);
    }

    @Override
    public Integer queryDkTaskCount(Map<String, Object> param) {
        return wpzfNdhcZgTbTaskMapper.queryDkTaskCount(param);
    }

    @Override
    public List<Map<String, Object>> queryDkTaskByIndex(Map<String, Object> param) {
        return wpzfNdhcZgTbTaskMapper.queryDkTaskByIndex(param);
    }

    @Override
    public Integer submitdate(List<Map<String, Object>> params) {
        Integer rest = 0;
        if (params != null && params.size() > 0) {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            Map<String, Object> map = new HashMap<>();
            map.put("user_name", loginUser.getUsername());
            map.put("nick_name", loginUser.getSysUser().getNickName());
            map.put("dwmc", loginUser.getSysUser().getDept().getDeptName());
            for (Map<String, Object> param : params) {
                map.put("dkbsm", param.get("dkbsm"));
                map.put("dkbh", param.get("dkbh"));
                map.put("xzqdm", param.get("xzqdm"));
                map.put("pch", param.get("pch"));
                map.put("ywhj", "3");
                map.put("sftj", "是");
                map.put("sftj_submitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));

                Map<String,Object> zgTbhcjg = wpzfNdhcZgTbTaskMapper.queryZgTbhcjg(map);
                map.put("zphlxjc", zgTbhcjg.get("zgTbhcjg"));
                map.put("yxzpdl", zgTbhcjg.get("yxzpdl"));
                wpzfNdhcZgTbTaskMapper.updateShtb(map);
                rest += wpzfNdhcZgTbTaskMapper.submitdate(map);
            }
        }
        return rest;
    }
}
